package cn.kai.ums.controller;

import cn.kai.ums.mapper.UmsUserMapper;
import cn.kai.ums.model.UmsUser;
import cn.kai.ums.service.UmsStudentService;
import cn.kai.ums.service.UmsTeacherService;
import cn.kai.ums.service.UmsUserService;
import cn.kai.ums.tools.JsonData;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("ums/v1/user")
@Api(tags = "用户控制类")
public class UserController {

    @Autowired
    private UmsUserService umsUserService;

    @Autowired
    private UmsUserMapper umsUserMapper;

    @Autowired
    private UmsStudentService umsStudentService;

    @Autowired
    private UmsTeacherService umsTeacherService;

    @ApiOperation("根据用户名和角色模糊查询")
    @GetMapping
    public JsonData getUserList(
            @RequestParam(defaultValue = "1",required = false) Integer pageNum
            ,@RequestParam(defaultValue = "5",required = false) Integer pageSize
            ,@RequestParam(defaultValue = "",value = "likeUser",required = false) String likeUser){
        Page<UmsUser> umsUserPage =umsUserMapper.selectPage(new Page<>(pageNum,pageSize)
                , Wrappers.<UmsUser>lambdaQuery()
                        .like(UmsUser::getUserName,likeUser)
                        .or()
                        .like(UmsUser::getUserRole,likeUser));
        return JsonData.buildSuccess(umsUserPage);
    }

    @PostMapping
    public JsonData saveUser(@RequestBody UmsUser user){
        String pwd = String.valueOf(new SimpleHash("MD5","000000",user.getUserName(),1024));
        user.setUserPwd(pwd);
        user.setUserCreate(new Date());
        user.setUserState(1);
        if(user.getUserName().length()==5){
            user.setUserRole("teacher");
        }else if(user.getUserName().length()==10){
            user.setUserRole("student");
        }else {
            user.setUserRole("admin");
        }
        return JsonData.buildSuccess(umsUserService.save(user));
    }

    @PutMapping
    public JsonData updateUser(@RequestBody UmsUser user){
        String pwd = String.valueOf(new SimpleHash("MD5",user.getUserPwd(),user.getUserName(),1024));
        user.setUserPwd(pwd);
        return JsonData.buildSuccess(umsUserService.updateById(user));
    }

    @DeleteMapping
    public JsonData delUser(@RequestParam Integer uid){
        return JsonData.buildSuccess(umsUserService.removeById(uid));
    }

    @PostMapping("/delBatchUser")
    public JsonData delBatchUser(@RequestBody List<Integer> uids){
        return JsonData.buildSuccess(umsUserService.removeByIds(uids));
    }


    @GetMapping("/getUserByToken")
    public JsonData getUserByToken(HttpServletRequest request,@RequestParam String token){
        Integer id = (Integer) request.getAttribute("user_id");
        return JsonData.buildSuccess(umsUserService.getById(id));
    }

    @ApiOperation("输入班级编号批量加入本班级中所有学生")
    @PostMapping("/batchSaveUserByStuNo")
    public JsonData batchSaveUserByStuNo(@RequestParam String clid){
        List<String> stunos = umsStudentService.getStuNosBycid(clid);
        for (String no:stunos){
            UmsUser user1 = umsUserService.getOneByname(no);
            if(user1==null){
                UmsUser user = new UmsUser();
                user.setUserName(no);
                String pwd = String.valueOf(new SimpleHash("MD5","000000",user.getUserName(),1024));
                user.setUserPwd(pwd);
                user.setUserCreate(new Date());
                user.setUserState(1);
                user.setUserRole("student");
                umsUserService.save(user);
            }

        }
        return JsonData.buildSuccess("000");
    }

    @ApiOperation("批量插入教师的用户信息")
    @PostMapping("/batchSaveUserByTeaNo")
    public JsonData batchSaveUserByTeaNo(){
        List<String> tea = umsTeacherService.getAllTno();
        for (String t:tea){
            UmsUser user1 = umsUserService.getOneByname(t);
            UmsUser user = new UmsUser();
            user.setUserName(t);
            String pwd = String.valueOf(new SimpleHash("MD5","000000",user.getUserName(),1024));
            user.setUserPwd(pwd);
            user.setUserCreate(new Date());
            user.setUserState(1);
            user.setUserRole("teacher");
            umsUserService.save(user);
        }
        return  JsonData.buildSuccess("000");
    }

    @ApiOperation("修改用户密码")
    @PutMapping("/updatePwd")
    public JsonData updatePwd(@RequestParam Integer uid, @RequestParam String password){
        UmsUser user = umsUserService.getById(uid);
        String pwd = String.valueOf(new SimpleHash("MD5",password,user.getUserName(),1024));
        user.setUserPwd(pwd);
        return JsonData.buildSuccess(umsUserService.updateById(user));
    }
}
